(function(Yo, YoPages){
	var Section = Yo.Section;
	var User = Yo.User;
	var Req = Yo.Req;
	var Utils = Yo.Utils;
	var Storage = Yo.Storage;

	// ajax filter
	Req.callbackFilter = function(data){
		if(!data)
			return true;

		if(data.flag){
			return true;
		}else{
			if(data.tips == 'user-login'){
				User.remove();
				alert('用户会话失效，请重新登陆！');
				return false;
			}else if(data.tips == 'parameter'){
				alert('参数错误！');
				return false;
			}

			return true;
		}
	};

	var showUser = function(user){
		$('#usernick').text(user.nickname);
	};

	var authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
	var redirectUrl = 'http://tech11.cn/wx/ind/shop/1';
	Section.authUrl = authUrl.format('wx9f7507a78aa0c8f0', encodeURIComponent(redirectUrl));

	YoPages.initPages.push(function(){
		User.remove();
		var user = window.userSaved;
		if(user){
			User.set(user);
			showUser(user);
		}else{
			document.location.href = Section.authUrl;
		}
	});

	// main page
	YoPages.initPages.push(function(){
		// 首页图片滑动
		$('.slider').on('slide', function(e){
			var slideNumber = e.detail.slideNumber;
			$('.slide-nav li').removeClass('selected').eq(slideNumber).addClass('selected');
		});

		$('.slide-group').on('click', 'img', function(e){
			var img = $(e.target);
			var params = Utils.str2params(img.attr('data-params'));
			Section.go('item-detail', false, params);
		});

		$('.item-list').on('click', '.info', function(e){
			var t = $(e.target);
			var li = t.closest('li');
			var params = Utils.str2params(li.attr('data-params'));
			Section.go('item-detail', false, params);
		});

		// 首页商品图片延迟加载
		var containerHeight = $('#main-content').height();

		$('#main-content').on('scroll', function(e){
			var index = 0;
			$('#item-list img[data-original]').each(function(){
				index++;

				var $img = $(this);
				if($img.data('loaded'))
					return;

				var topImg = $img.offset().top;
				if(topImg < containerHeight){
					$img.data('loaded', true);
					$img.attr("src", $img.attr('data-original'));

					Yo.log.i('show image - ' + index);
				}
			});
		}).triggerHandler('scroll');
	});

	// ** item detail
	YoPages.initPages.push(function(){
		// cache in local，memory or session storage
		Section.items = new Storage();

		var showItem = function(data){
			$('#comment-number').text(data.commentNum);

			$('#item-detail-main').html(data.innerMain);
			$('#item-detail-comment').html(data.innerComment);

			Section.currentItemId = data.id;
		};

		Section.addPageFn('item-detail', function(params){
			if(!params || !params.id)
				return;

			var id = params.id;
			if(Section.currentItemId == id){
				return;
			}

			// cache local 1mins
			var expired = 1000 * 60;
			var item = Section.items.get(id);
			if(item && item.lastUpdateTime + expired > new Date().getTime()){
				showItem(item);
				return;
			}

			Req.get('/wx/ind/shop/item/' + id, {}, function(data){
				if(!data.innerMain)
					return;

				data.lastUpdateTime = new Date().getTime();
				Section.items.set(id, data);

				showItem(data);
			});
		});

		// main or comment show
		$('#itemDetailNavMain').click(function(e){
			$('#item-detail-comment').hide();
			$('#item-detail-main').show();

			var navs = $('#item-detail header div');
			navs.eq(0).addClass('current');
			navs.eq(1).removeClass('current');
		});

		$('#itemDetailNavComment').click(function(e){
			$('#item-detail-main').hide();
			$('#item-detail-comment').show();

			var navs = $('#item-detail header div');
			navs.eq(1).addClass('current');
			navs.eq(0).removeClass('current');
		});
	});
})(this.Yo, this.YoPages);